import { IRouteComponentProps, Redirect } from 'umi';

import { menuList } from '@/pages/admin/components/MenuList';

export default function Layout({
  children,
  location,
  route,
  history,
  match,
}: IRouteComponentProps) {
  if (location.pathname === '/') {
    return <Redirect to="/login"></Redirect>;
  } else {
    if (location.pathname === '/login') {
      return children;
    }
    if (location.pathname === '/admin') {
      return <Redirect to="/admin/welcome"></Redirect>;
    }
    // val就是path对应的列表的项
    const val = menuList.find((item) => {
      return item.key === location.pathname;
    });
    const anth = JSON.parse(sessionStorage.getItem('auth') || '[]');
    if (val && anth.includes(+val.auth)) {
      return children;
    } else {
      return <Redirect to="/login"></Redirect>;
    }
  }
}
